/*
 *    Copyright (c) 2018-2025, cloud All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: cloud
 */

package com.fuyao.cloud.asset.card.controller;

import cn.hutool.core.io.IoUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fuyao.cloud.asset.entity.AssetCard;
import com.fuyao.cloud.asset.enums.CardStatusEnum;
import com.fuyao.cloud.asset.vo.card.AssetCardImportVO;
import com.fuyao.cloud.asset.vo.card.AssetCardSearchDTO;
import com.fuyao.cloud.common.core.util.R;
import com.fuyao.cloud.common.data.mybatis.SpliceQueryWrapper;
import com.fuyao.cloud.common.excel.annotation.RequestExcel;
import com.fuyao.cloud.common.log.annotation.SysLog;
import com.fuyao.cloud.asset.card.service.AssetAssetcardService;
import lombok.SneakyThrows;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.access.prepost.PreAuthorize;
import com.fuyao.cloud.common.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 资产信息管理
 *
 * @author liyue
 * @date 2021-11-18 18:44:56
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/fyassetassetcard")
@Api(value = "fyassetassetcard", tags = " 资产信息管理管理")
public class AssetAssetcardController {

	private final AssetAssetcardService fyAssetAssetcardService;

	/**
	 * 分页查询
	 *
	 * @param page             分页对象
	 * @param fyAssetCard 资产信息管理
	 * @return R
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('cord_fyassetassetcard_view')")
	public R getFyAssetAssetCardPage(Page page, AssetCardSearchDTO fyAssetCard) {
		QueryWrapper queryWrapper = SpliceQueryWrapper.spliceSearchCondition(fyAssetCard);
		queryWrapper.notIn("status", CardStatusEnum.OFFSET.getCode());
		return R.ok(fyAssetAssetcardService.page(page,queryWrapper));
	}

	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/assetCardPage")
	@PreAuthorize("@pms.hasPermission('cord_fyassetassetcard_view')")
	public R assetCardPage(Page page, AssetCardSearchDTO searchDTO) {
		return R.ok(fyAssetAssetcardService.getAssetCardPage(page, SpliceQueryWrapper.spliceSearchCondition(searchDTO)));
	}


	/**
	 * 通过id查询 资产信息管理
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('cord_fyassetassetcard_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(fyAssetAssetcardService.getById(id));
	}

	/**
	 * 新增 资产信息管理
	 *
	 * @param fyAssetAssetcard 资产信息管理
	 * @return R
	 */
	@ApiOperation(value = "新增 资产信息管理", notes = "新增 资产信息管理")
	@SysLog("新增 资产信息管理")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('cord_fyassetassetcard_add')")
	public R save(@RequestBody AssetCard fyAssetAssetcard) {
		return R.ok(fyAssetAssetcardService.save(fyAssetAssetcard));
	}

	/**
	 * 修改 资产信息管理
	 *
	 * @param fyAssetAssetcard 资产信息管理
	 * @return R
	 */
	@ApiOperation(value = "修改 资产信息管理", notes = "修改 资产信息管理")
	@SysLog("修改 资产信息管理")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('cord_fyassetassetcard_edit')")
	public R updateById(@RequestBody AssetCard fyAssetAssetcard) {
		return R.ok(fyAssetAssetcardService.updateById(fyAssetAssetcard));
	}

	/**
	 * 通过id删除 资产信息管理
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除 资产信息管理", notes = "通过id删除 资产信息管理")
	@SysLog("通过id删除 资产信息管理")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('cord_fyassetassetcard_del')")
	public R removeById(@PathVariable Long id) {
		return R.ok(fyAssetAssetcardService.removeById(id));
	}


	/**
	 * 导出excel 表格
	 *
	 * @param fyAssetCard 查询条件
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('cord_fyassetassetcard_export')")
	public List export(AssetCardSearchDTO fyAssetCard) {
		return fyAssetAssetcardService.listAssetCard(SpliceQueryWrapper.spliceSearchCondition(fyAssetCard));
	}

	/**
	 * 查询同合树
	 *
	 * @return 合同树结构
	 */
	@GetMapping(value = "/tree")
	public R getTree(AssetCard card) {
		return R.ok(fyAssetAssetcardService.selectTree(Wrappers.query(card)));
	}

	/**
	 * 下载本地导入模板
	 * 获取本地（resources）文件
	 *
	 * @param fileName 文件名称
	 * @param response 本地文件
	 */
	@SneakyThrows
	@GetMapping("/local/{fileName}")
	@PreAuthorize("@pms.hasPermission('cord_fyassetassetcard_export')")
	public void localFile(@PathVariable String fileName, HttpServletResponse response) {
		ClassPathResource resource = new ClassPathResource("file/" + fileName);
		response.setContentType("application/octet-stream; charset=UTF-8");
		IoUtil.copy(resource.getInputStream(), response.getOutputStream());
	}

	/**
	 * 导入资产分类信息信信息
	 *
	 * @param excelList     区域列表
	 * @param bindingResult 错误信息列表
	 * @return R
	 */
	@PostMapping("/import")
	@PreAuthorize("@pms.hasPermission('cord_fyassetassetcard_export')")
	public R importCti(@RequestExcel List<AssetCardImportVO> excelList, BindingResult bindingResult) {
		return this.fyAssetAssetcardService.importAssetCard(excelList, bindingResult);
	}

	/**
	 * 资产信息创建方法
	 *
	 * @return 资产信息
	 */
	@GetMapping(value = "/create")
	public R create() {
		return R.ok(fyAssetAssetcardService.create());
	}

	@GetMapping("/edit/{id}")
	@PreAuthorize("@pms.hasPermission('cord_fyassetassetcard_edit')")
	public R edit(@PathVariable("id") Long id) {
		return R.ok(this.fyAssetAssetcardService.edit(id));
	}

	@ApiOperation(value = "批量删除资产信息", notes = "批量删除资产信息")
	@SysLog("批量删除资产信息")
	@DeleteMapping("/batchDelete")
	@PreAuthorize("@pms.hasPermission('cord_fyassetassetcard_del')")
	public R batchDelete(@RequestBody List<Long> ids) {
		return fyAssetAssetcardService.batchDelete(ids);
	}

}
